﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using GameFramework;
using SuperScrollView;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;

namespace Adventure
{
    public class ShopForm : UGuiForm
    {

        private LoopListView2 mScrollView;
        private ScrollRect mScrollRect;

        private List<ShopData> datas;

        
        protected override void OnInit(object userData)
        {
            base.OnInit(userData);

            datas = ShopData.Initialize();

            mScrollView = transform.FindTransform<LoopListView2>("Scroll View");
            mScrollView.InitListView(datas.Count, GetScrollViewUpdate);

            mScrollView.SupportScrollBar = false;
            mScrollView.ItemSnapEnable = false;

            mScrollRect = mScrollView.GetComponent<ScrollRect>();
            mScrollRect.horizontal = false;
            mScrollRect.vertical = true;
            mScrollRect.horizontalScrollbar = null;
            mScrollRect.verticalScrollbar = null;

            transform.FindTransform<UIButton>("MaskPanel").onClick.AddListener(() =>
            {
                GameEntry.Sound.PlayUISound(10001);

                GameEntry.UI.OpenUIForm(UIFormId.MenuForm, GameEntry.Procedure.CurrentProcedure);
                Close();
                
            });
        }

        protected override void OnOpen(object userData)
        {
            base.OnOpen(userData);
            mScrollView.MovePanelToItemIndex(0,0);
        }
     
        private LoopListViewItem2 GetScrollViewUpdate(LoopListView2 listView, int index)
        {
            if (index < 0 || index >= datas.Count) return null;

            var Item2 = listView.NewListViewItem("Item");

            //item2对其获取或者添加 不能直接引用，需要 dot出其gameObject，要不然分不清哪个是哪个，重复利用的情况。dmeo示例里面是直接手动添加Item，所以不存在这个问题。
            //注意：再次之前需要先获取到数据！！！，不能用一个空的数据代替着先初始化
            ShopItem scriptItem = Item2.gameObject.GetComponent<ShopItem>();
            if (scriptItem == null) scriptItem = Item2.gameObject.AddComponent<ShopItem>();

            if (Item2.IsInitHandlerCalled == false)
            {
                Item2.IsInitHandlerCalled = true;
                scriptItem.Init();
            }
            scriptItem.SetItemData(datas[index], index);

            return Item2;
        }
    }

    public class ShopItem : MonoBehaviour
    {

        public ShopData data;

        public Text NameText;
        public Text GoldText;

        public Text InfoText;
        public UIButton uIButton;
        public void Init()
        {
            NameText = transform.FindTransform<Text>("NameText");
            GoldText = transform.FindTransform<Text>("GoldText");
            InfoText = transform.FindTransform<Text>("InfoText");

            uIButton = transform.FindTransform<UIButton>("BuyButton");
            uIButton.onClick.AddListener(()=>
            {
                GameEntry.Sound.PlayUISound(10001);

                if (GameEntry.ArchiveData.Data.Gold>=data.gold)
                {

                    string content = GameEntry.Localization.GetString("ShopForm.BuySucceed",NameText.text);
                    int colorCode = 2;
                    GameEntry.UI.OpenUIForm(UIFormId.TipForm, Tuple.Create(content, colorCode));

                    //金币减少
                    GameEntry.ArchiveData.Data.Gold -= data.gold;
                    //成就
                    GameEntry.ArchiveData.Data.ShopCount++;

                    //背包数量增加
                    if (data.Name== "Common.YaoShui_10")
                    {
                        GameEntry.ArchiveData.Data.playerBackpacker.YaoShuiNum_10+=1;
                    }
                    if (data.Name == "Common.YaoShui_20")
                    {
                        GameEntry.ArchiveData.Data.playerBackpacker.YaoShuiNum_20++;
                    }
                    if (data.Name == "Common.YaoShui_50")
                    {
                        GameEntry.ArchiveData.Data.playerBackpacker.YaoShuiNum_50++;
                    }
                    if (data.Name == "Common.EXP_10")
                    {
                        GameEntry.ArchiveData.Data.playerBackpacker.EXPNum_10++;
                    }
                    if (data.Name == "Common.EXP_20")
                    {
                        GameEntry.ArchiveData.Data.playerBackpacker.EXPNum_20++;
                    }
                    if (data.Name == "Common.EXP_50")
                    {
                        GameEntry.ArchiveData.Data.playerBackpacker.EXPNum_50++;
                    }
                }
                else
                {
                    string content = GameEntry.Localization.GetString("ShopForm.BuyErro", NameText.text);
                    int colorCode = 1;
                    GameEntry.UI.OpenUIForm(UIFormId.TipForm, Tuple.Create(content, colorCode));
                }

                GameEntry.TimerComponent.AddOnceTimer(1000, () => { GameEntry.UI.CloseUIForm(GameEntry.UI.GetUIForm(UIFormId.TipForm)); });
            });
        }

        public void SetItemData(object arg, int index)
        {
            var vo = (ShopData)arg;
            data = vo;
            NameText.text = GameEntry.Localization.GetString(vo.Name);
            GoldText.text = GameEntry.Localization.GetString("ShopForm.Gold", vo.gold);
            InfoText.text = GameEntry.Localization.GetString(vo.Info);
        }
    }
    public struct ShopData
    {
        public int gold;
        public string Name;
        public string Info;

        public static List<ShopData> Initialize()
        {
            var vo = new List<ShopData>()
        {
            new ShopData ()
            {
                Name="Common.YaoShui_10",
                gold=10,
                Info="Common.YaoShuiInfo_10",

            },
             new ShopData ()
            {
                Name="Common.YaoShui_20",
                gold=20,
                Info="Common.YaoShuiInfo_20",

            },
                 new ShopData ()
            {
                Name="Common.YaoShui_50",
                gold=999999,
                Info="Common.YaoShuiInfo_50",

            },
                       new ShopData ()
            {
                Name="Common.EXP_10",
                gold=10,
                Info="Common.EXPInfo_10",

            },
                                       new ShopData ()
            {
                Name="Common.EXP_20",
                gold=20,
                Info="Common.EXPInfo_20",

            },
                                new ShopData ()
            {
                Name="Common.EXP_50",
                gold=50,
                Info="Common.EXPInfo_50",

            },
        };

            return vo;
        }
    }

}


